//
//  HandInPaymentVC.m
//  MyNewProject
//
//  Created by sun on 2019/2/12.
//  Copyright © 2019 sun. All rights reserved.
//
#import "HandInPaymentVC.h"
#import "HandInPaymentSuccessVC.h"
#import "PayDataModels.h"
#import "AddressListVC.h"
#import "XM_AuctionProductModel.h"
#import "NSString+Helper.h"
#import "SignViewController.h"
#import "XMRechargeViewController.h"
#import "CouponViewController.h"
#import "XMAlertView.h"

#pragma mark -- mark 数据相关
#import "UserInfoModel.h"
#import "MyCouponModel.h"

@interface HandInPaymentVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
    UIButton *_aliPaybtn;
    UIButton *_wxPaybtn;
    UIButton *_balancePaybtn;
}
@property(nonatomic,strong)UITableView * myTalbleView;
@property (nonatomic, strong) UILabel *balanceLabel;
@property (nonatomic, assign) CGFloat balanceMoney;
@property (nonatomic, copy) NSString *addr_id;
@property (nonatomic, assign) CGFloat total_money; // 合计

@property (nonatomic, strong) UIImage *signImage; // 签名图片

/**
 优惠券
 */
@property (nonatomic, strong) MyCouponDataModel *couponModel;
@property (nonatomic, strong) NSArray <MyCouponDataModel *>* couponModels;
@property (nonatomic, strong) PayData *myData;
@end

@implementation HandInPaymentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *title = @"";
    switch (self.type) {
        case PayTypeScore:
            title = @"积分兑换";
            break;
        case PayTypeProduct:
            title = @"支付";
            break;
        case PayTypeAuction:
            title = @"尾款补交";
            break;
        default:
            break;
    }
    self.gk_navTitle = title;
    [self initView];
    [self getAddressListInfo];
}

-(void)initView {
    [self.view addSubview:self.myTalbleView];
    UIFont * font1 = [UIFont systemFontOfSize:16];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, SCREENH_HEIGHT -50 - SKBottomSafe_Height, SCREEN_WIDTH , 50);
    NSString *title = (self.type == PayTypeScore) ? @"积分兑换" : @"去支付";
    [button1 setTitle:title forState:UIControlStateNormal];
    button1.backgroundColor = SKColorWithHex(0xE33030);
    button1.titleLabel.font = font1 ;
    [button1 setTitleColor:SKColorWithHex(0xFFFFFF) forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(payDeal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:button1];
}

/**
 去支付订单
 */
- (void)payOrderInfo {
    NSString *msg = self.type == PayTypeScore ? @"是否确认兑换" : @"是否确认支付订单";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    SKWeakSelf;
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf makeSurePay];
    }];
    [alert addAction:cancel];
    [alert addAction:sure];
    [self presentViewController:alert animated:true completion:nil];
}

/// 支付
- (void)makeSurePay {
    if (IsStrEmpty(self.myData.addr.ID)) {
        [self.view makeToast:@"请选择地址" duration:1 position:CSToastPositionCenter];
        return; }
    SKWeakSelf;
    NSDictionary *dic = @{@"uid":self.uid,
                            @"loginKey":self.loginKey,
                            @"id":self.idStr,
                            @"addr_id":self.myData.addr.ID,
                            @"type":@(self.type)
                            };
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:dic];
    if (self.couponModel) {
        [param addEntriesFromDictionary:@{@"shop_coupon_id":self.couponModel.ID}];
    }
    if (self.type == PayTypeAuction) {
        // 拍卖 必须要 签名
        if (self.signImage) {
            NSData *data = UIImageJPEGRepresentation(self.signImage, 0.1);
            [YQNetworking uploadFileWithUrl:@"pay/setOrder" parameters:param fileData:data type:@"file.jpg" name:@"img" mimeType:@"image/jpeg" progressBlock:^(int64_t bytesWritten, int64_t totalBytes) {
                
            } successBlock:^(id response) {
                // 支付成功 失败
                [weakSelf payResultWithInfo:response];
            } failBlock:^(NSError *error) {
                [weakSelf debugShowMessage:error.localizedDescription];
            }];
        }else {
            SignViewController *signVC = [[SignViewController alloc] init];
            signVC.signResult = ^(UIImage *signImage) {
                weakSelf.signImage = signImage;
                [weakSelf payOrderInfo];
            };
            [self.navigationController pushViewController:signVC animated:YES];
        }
    }else {
        [YQNetworking postWithUrl:@"pay/setOrder" params:param reloginRefreshBlock:^(id response) {
            
        } successBlock:^(id response) {
           // 支付成功 失败
            [weakSelf payResultWithInfo:response];
        } failBlock:^(NSError *error) {
            [weakSelf debugShowMessage:error.localizedDescription];
        }];
    }
}
/**
 支付结果

 @param response response
 */
- (void)payResultWithInfo:(NSDictionary *)response {
    WJBaseModel *model = [WJBaseModel modelWithDictionary:response];
    if ([model.status isEqualToString:@"lessmoney"]) {
        // 余额不足
        XMAlertView * alertView = [[XMAlertView alloc]initWithAlertViewWithMsg:@"您的账户余额不足"];
        [alertView show];
        SKWeakSelf;
        alertView.leftBlock = ^{
            // 去充值
            [weakSelf rechargeButtonClick:nil];
        };
    } else if ([model.status isEqualToString:@"lessjifen"]) {
        if ([model.msg containsString:@"余额不足"]) {
            // 余额不足
            XMAlertView * alertView = [[XMAlertView alloc]initWithAlertViewWithMsg:@"您的账户余额不足"];
            [alertView show];
            SKWeakSelf;
            alertView.leftBlock = ^{
                // 去充值
                [weakSelf rechargeButtonClick:nil];
            };
        }else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"积分不足\n参与竞拍可获取更多积分" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:nil];
            [alert addAction:cancel];
            [self presentViewController:alert animated:true completion:^{
                
            }];
        }
    }else {
        HandInPaymentSuccessVC *vc = [[HandInPaymentSuccessVC alloc] init];
        if ([model isSuccess]) {
            vc.myOrderState = OrderStateSuccess;
            vc.count = response[@"data"][@"count"];
            vc.up_jifen = response[@"data"][@"up_jifen"];
            vc.type = response[@"data"][@"type"];
        }else {
            vc.myOrderState = OrderStateFailure;
        }
        [self.navigationController pushViewController:vc animated:true];
    }
}
/**
 初始化支付订单
 */
- (void)getPayInfo {
    NSDictionary *param = @{
                            @"uid":self.uid,
                            @"loginKey":self.loginKey,
                            @"id":self.idStr,
                            @"addr_id":self.addr_id,
                            @"type":@(self.type)
                            };
    SKWeakSelf;
    [YQNetworking postWithUrl:@"pay/OrderPay" params:param reloginRefreshBlock:^(id response) {

    } successBlock:^(id response) {
        PayBaseClass * baseClass = [PayBaseClass modelWithDictionary:response];
        if ([baseClass.status isEqualToString:@"ok"]) {
            weakSelf.myData = baseClass.data;
            weakSelf.total_money = baseClass.data.amount.count;
            weakSelf.couponModels = baseClass.data.shop_coupon;
            [self initView];
            [self.myTalbleView reloadData];
        }
        else { }
        [weakSelf getBalanceInfo];
    } failBlock:^(NSError *error) {
        [weakSelf getBalanceInfo];
    }];
}

- (void)getBalanceInfo {
    SKWeakSelf;
    [YQNetworking getWithUrl:@"my/getUserInfo" params:@{@"uid":self.uid, @"loginKey":self.loginKey} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        UserInfoModel *model = [UserInfoModel modelWithDictionary:response];
        if ([model isSuccess]) {
            // 登录状态
            if (weakSelf.type == PayTypeScore) {
                // 积分
                weakSelf.balanceLabel.text = [NSString stringWithFormat:@"可用积分：%@", model.user_info.jifen];
            }else {
                // 余额
                weakSelf.balanceLabel.text = [NSString stringWithFormat:@"可用金额：%@", model.user_info.keyong_yue];
                weakSelf.balanceMoney = [model.user_info.keyong_yue floatValue];
            }
            YPLoginUserInfo *info = [YPLoginUserInfo modelObjectWithDictionary: model.user_info.modelToJSONObject];
            [UserInfoSingleton shareInstance].userInfo = info;
            SKUserUserDefaults_Save(response[@"user_info"] , @"UserInfo");
        }else {
            // 登陆失效
            [weakSelf showMessage:model.msg];
        }
    } failBlock:^(NSError *error) {
        
    }];
}
- (void)getAddressListInfo {
    SKWeakSelf;
    [YQNetworking getWithUrl:@"My/getAddrList" params:@{@"uid":self.uid, @"loginKey":self.loginKey} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        if ([response[@"status"] isEqualToString:@"ok"]) {
            NSArray * dataArray = response[@"data"];
            for (NSDictionary * dic in dataArray) {
                XM_MyAddressModel * pModel =  [XM_MyAddressModel modelWithDictionary:dic];
                weakSelf.addr_id = pModel.addrId;
            }
            for (NSDictionary *dic in dataArray) {
                XM_MyAddressModel * pModel =  [XM_MyAddressModel modelWithDictionary:dic];
                if ([pModel.is_default isEqualToString:@"1"]) {
                    weakSelf.addr_id = pModel.addrId;
                }
            }
        }else {
            weakSelf.addr_id = @"";
        }
        [weakSelf getPayInfo];
    } failBlock:^(NSError *error) {
        [weakSelf getPayInfo];
    }];
}

#pragma mark -- 去支付
-(void)payDeal:(UIButton *)sender {
    [self payOrderInfo];
}

-(UITableView*)myTalbleView {

    if (_myTalbleView ==nil) {
        _myTalbleView =[[UITableView alloc] initWithFrame:CGRectMake(0,SKTopBar_Height ,SCREEN_WIDTH , SCREENH_HEIGHT-SKTopBar_Height-50 - SKBottomSafe_Height) style:UITableViewStyleGrouped];
        _myTalbleView.dataSource = self;
        _myTalbleView.delegate = self;
        _myTalbleView.estimatedRowHeight = 0;
        _myTalbleView.estimatedSectionFooterHeight = 0;
        _myTalbleView.estimatedSectionHeaderHeight = 0;
        _myTalbleView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            _myTalbleView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _myTalbleView;
}
#pragma mark -  UITableView 代理相关

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberOfRows = 0;
    switch (section) {
        case 0:
        {
            numberOfRows = 1;
        }
            break;
        case 1:
        {
            if ((self.type == PayTypeScore)||(self.type == PayTypeProduct)) {
                numberOfRows = 4;
            }else {
                numberOfRows = 3;
            }
        }
            break;
        case 2:
        {
            numberOfRows = 1;
        }
            break;
        case 3:
        {
            numberOfRows = 1;
        }
            break;
        default:
            break;
    }
    
    return numberOfRows;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    for (id view in [cell.contentView subviews]) {
        [view removeFromSuperview];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    switch (indexPath.section) {
        case 0:
        {
            NSString * nameStr = [NSString stringWithFormat:@"%@ %@",SafeString(self.myData.addr.name),SafeString(self.myData.addr.tel)];
            NSString * addressText  =[NSString stringWithFormat:@"%@ %@",SafeString(self.myData.addr.city),SafeString(self.myData.addr.addr)];
            if (IsStrEmpty([nameStr trimString])&&IsStrEmpty([addressText trimString])) {
                UIFont * font1 = [UIFont systemFontOfSize:16];
                UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
                button1.frame = CGRectMake((SCREEN_WIDTH -80)/2,25 , 100 , 40);
                [button1 setTitle:@"选择地址" forState:UIControlStateNormal];
                button1.backgroundColor = SKColorWithHex(0xE33030);
                button1.userInteractionEnabled = NO;
                button1.titleLabel.font = font1 ;
                button1.layer.masksToBounds = YES;
                button1.layer.cornerRadius = 5;
                [button1 setTitleColor:SKColorWithHex(0xFFFFFF) forState:UIControlStateNormal];
                [cell.contentView addSubview:button1];
            }else {
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, SCREEN_WIDTH - 35, 25)];
                label.textColor = SKColorWithHex(0x444444);
                label.font = SKFONT_Semibold(16);
                label.text =nameStr;
                label.textAlignment = NSTextAlignmentLeft;
                [cell.contentView addSubview:label];
                
                UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, label.bottom+5, 10, 13)];
                imageView.image = SKIMAGE_NAMED(@"addressicon");
                
                [cell.contentView addSubview:imageView];
                
                CGFloat height = [NSString getHeightWithString:addressText font:SKFONT_Medium(14) lineBreakModel:NSLineBreakByTruncatingTail limitWidth:SCREEN_WIDTH - 35 -15 -20-10];
                UILabel * addressLab = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right+3, label.bottom, SCREEN_WIDTH - 35 -15 -20-10, height)];
                addressLab.numberOfLines = 0;
                addressLab.textColor = SKColorWithHex(0x444444);
                addressLab.font = SKFONT_Medium(14);
                addressLab.text = addressText;
                addressLab.textAlignment = NSTextAlignmentLeft;
                [cell.contentView addSubview:addressLab];
                
                UIImageView * arrowImageView =[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -15 -6, label.bottom +(height-11)/2, 6, 11)];
                arrowImageView.image = SKIMAGE_NAMED(@"rightArrow");
                [cell.contentView addSubview:arrowImageView];
            }
        }
            break;
        case 1:
        {
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, 150, 50)];
            label.font = SKFONT_Semibold(14.0);
            label.textAlignment = NSTextAlignmentLeft;
            [cell.contentView addSubview:label];


            UILabel * priceLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-25 -200, 0, 200, 50)];
            priceLab.font = SKFONT_Semibold(13.0);
            priceLab.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:priceLab];
            switch (indexPath.row) {
                case 0:
                {
                    label.textColor = SKColorWithHex(0x999999);
                    label.text = @"商品总价";
                    priceLab.textColor = SKColorWithHex(0x999999);
                    priceLab.text = [NSString stringWithFormat:@"￥%.2lf",[self.myData.amount.money floatValue]];
                    UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(25, 49.5, SCREEN_WIDTH -50, 0.5)];
                    imageView.backgroundColor =SKRGBColor(237, 237, 237);
                    [cell.contentView addSubview:imageView];
                }
                    break;
                case 1:
                {
                    label.textColor = SKColorWithHex(0x999999);
                    label.text = @"运费";
                    priceLab.textColor = SKColorWithHex(0x999999);
                    priceLab.text = [NSString stringWithFormat:@"￥%.2f",self.myData.amount.freight];
                    UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(25, 49.5, SCREEN_WIDTH -50, 0.5)];
                    imageView.backgroundColor =SKRGBColor(237, 237, 237);
                    [cell.contentView addSubview:imageView];
                }
                    break;
                case 2:
                {
                    switch (self.type) {
                        case PayTypeScore:
                        {
                            label.textColor = SKColorWithHex(0x999999);
                            label.text = @"积分";

                            priceLab.textColor = SKColorWithHex(0x999999);
                            priceLab.text = [NSString stringWithFormat:@"%.0f",self.myData.amount.jifen];
                            UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(25, 49.5, SCREEN_WIDTH -50, 0.5)];
                            imageView.backgroundColor =SKRGBColor(237, 237, 237);
                            [cell.contentView addSubview:imageView];
                        }
                            break;
                        case PayTypeAuction:
                        {
                            label.textColor = SKColorWithHex(0x4F4F4F);
                            label.text = @"合计";
                            priceLab.textColor = SKColorWithHex(0x000000);
                            priceLab.text = [NSString stringWithFormat:@"￥%.2lf",self.myData.amount.count];
                        }
                            break;
                        case PayTypeProduct:
                        {
                            label.textColor = SKColorWithHex(0x999999);
                            label.text = @"优惠券";
                            priceLab.textColor = SKColorWithHex(0xE33030);
                            if (self.couponModel) {
                                priceLab.text = [NSString stringWithFormat:@"满%@减%@优惠券", self.couponModel.tiaojian, self.couponModel.money];
                            }else {
                                priceLab.text = @"选择优惠券";
                            }
                            UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(25, 49.5, SCREEN_WIDTH -50, 0.5)];
                            imageView.backgroundColor =SKRGBColor(237, 237, 237);
                            [cell.contentView addSubview:imageView];
                            UIImageView *allowImageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"rightArrow")];
                            [cell.contentView addSubview:allowImageView];
                            allowImageView.center = CGPointMake(priceLab.right + allowImageView.size.width, priceLab.centerY);
                        }
                        default:
                            break;
                    }
                }
                    break;
                case 3:
                {
                    label.textColor = SKColorWithHex(0x4F4F4F);
                    label.text = @"合计";
                    priceLab.textColor = SKColorWithHex(0x000000);
                    priceLab.text = [NSString stringWithFormat:@"￥%.2lf",self.myData.amount.count];
                    if (self.type == PayTypeProduct) {
                        if (self.couponModel) {
                            CGFloat money = self.total_money - [self.couponModel.money floatValue];
                            priceLab.text = [NSString stringWithFormat:@"￥%.2lf", money];
                        }
                    }
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            UILabel * label = [[UILabel alloc] init];
            label.textColor = SKColorWithHex(0x444444);
            label.font = SKFONT_Semibold(18);
            label.textAlignment = NSTextAlignmentLeft;
            label.text = @"请选择支付方式";
            [cell.contentView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(15);
                make.height.mas_equalTo(25);
            }];
//            NSArray *titles = @[@"支付宝支付",
//                                @"微信支付",
//                                @"余额支付"];
//            NSArray *imageNames = @[@"mine_payment_alipay_type_img",
//                                    @"mine_payment_wx_type_img",
//                                    @"mine_payment_balance_type_img"];
            
            NSArray *titles;
            if (self.type == PayTypeScore) {
                titles = @[@"积分兑换"];
            }else {
                titles = @[@"余额支付"];
            }
            NSArray *imageNames = @[
                                    @"mine_payment_balance_type_img"];
            UIFont *font = SKFONT_SIZE(14);
            UIColor *color = SKColorWithHex(0x444444);
            for (int i = 0; i < titles.count; i++) {
                NSString *title = titles[i];
                NSString *imageName = imageNames[i];
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setTitle:title forState:UIControlStateNormal];
                [button setImage:SKIMAGE_NAMED(imageName) forState:UIControlStateNormal];
                [button setImagePosition:LXMImagePositionLeft spacing:10];
                [cell.contentView addSubview:button];
                [button setTitleColor:color forState:UIControlStateNormal];
                button.titleLabel.font = font;
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(label);
                    make.top.mas_equalTo(label.mas_bottom).mas_offset(10 + i * (40 + 10));
                    make.height.mas_equalTo(40);
                }];
                UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [selectBtn setImage:SKIMAGE_NAMED(@"unSelectedItem") forState:UIControlStateNormal];
                [selectBtn setImage:SKIMAGE_NAMED(@"selectedItem") forState:UIControlStateSelected];
//                if (i == 0) {
//                    _aliPaybtn = selectBtn;
//                }else if (i == 1) {
//                    _wxPaybtn = selectBtn;
//                }else if (i == 2) {
//                    _balancePaybtn = selectBtn;
//                }else {}
                _balancePaybtn = selectBtn;
                [selectBtn addTarget:self action:@selector(choosePaytypeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:selectBtn];
                selectBtn.selected = true;
                [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_offset(-15);
                    make.centerY.mas_equalTo(button);
                    make.size.mas_equalTo(CGSizeMake(20, 20));
                }];
//                if (i == 2) {
                if (i == 0) {
                    // 余额支付
                    UILabel *label = [[UILabel alloc] init];
                    label.text = [NSString stringWithFormat:@"可用余额：%.2lf",[UserInfoSingleton shareInstance].userInfo.keyong_yue.doubleValue];
                    label.textColor = SKColorWithHex(0x999999);
                    label.font = SKFONT_SIZE(14);
                    [cell.contentView addSubview:label];
                    self.balanceLabel = label;
                    [label mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.right.mas_equalTo(selectBtn.mas_left).mas_offset(-15);
                        make.centerY.mas_equalTo(selectBtn);
                    }];
                    
                    UIButton *rechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    [rechargeButton setTitle:@"充值" forState:UIControlStateNormal];
                    [rechargeButton setTitleColor:SKColorWithHex(0xE33030) forState:UIControlStateNormal];
                    [rechargeButton addTarget:self action:@selector(rechargeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                    rechargeButton.titleLabel.font = font;
                    [cell.contentView addSubview:rechargeButton];
                    [rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.right.mas_equalTo(label.mas_left).mas_offset(-15);
                        make.centerY.mas_equalTo(label);
                    }];
                }
            }
        }
            break;
        case 3:
        {
            NSString * labText = @"备注";
            UILabel * label = [[UILabel alloc] init];
            label.textColor = SKColorWithHex(0x444444);
            label.font = SKFONT_Semibold(14);
            label.textAlignment = NSTextAlignmentLeft;
            label.text =labText;
            [cell.contentView addSubview:label];
            CGFloat labWidth = [NSString getWidthWithString:labText font:label.font lineBreakModel:NSLineBreakByCharWrapping];
            label.frame = CGRectMake(25, 0, labWidth, 50);
            UITextField * textField =[[UITextField alloc] initWithFrame:CGRectMake(labWidth+25 +10, 0, SCREEN_WIDTH-50, 50)];
            textField.font = SKFONT_Semibold(14);
            textField.textColor = SKColorWithHex(0x444444);
            textField.delegate = self;
            NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"备注" attributes: @{NSForegroundColorAttributeName:SKColorWithHex(0x999999),
                                                NSFontAttributeName:SKFONT_Semibold(14)
                                                }];
            textField.attributedPlaceholder = attrString;
            [cell.contentView addSubview:textField];
        }
            break;

        default:
            break;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            
            NSString * nameStr = [NSString stringWithFormat:@"%@ %@",SafeString(self.myData.addr.name),SafeString(self.myData.addr.tel)];
            
            NSString * addressText  =[NSString stringWithFormat:@"%@ %@",SafeString(self.myData.addr.city),SafeString(self.myData.addr.addr)];
            
            if (IsStrEmpty([nameStr trimString])&&IsStrEmpty([addressText trimString]))
            {
                return 90;
            }
//            NSString * addressText  =[NSString stringWithFormat:@"%@ %@",SafeString(self.myData.addr.city),SafeString(self.myData.addr.addr)];
            CGFloat height = [NSString getHeightWithString:addressText font:SKFONT_Medium(14) lineBreakModel:NSLineBreakByTruncatingTail limitWidth:SCREEN_WIDTH - 35 -15 -20];

            return 15 +25 + height +15;
        }
            break;
        case 1:
        {
            return 50;
        }
            break;
        case 2:
        {
//            return 15 + 25 + ((10 + 40) * 3) +15; // 三个选项
            return 15 + 25 + ((10 + 40) * 1) +15; // 三个选项
        }
            break;
        case 3:
        {
            return 50;
        }
            break;
        default:
            break;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SKWeakSelf;
        AddressListVC * addressListVC = [[AddressListVC alloc] init];
        addressListVC.kindOfType = 1;
        addressListVC.myAdressList = ^(XM_MyAddressModel * model) {
            weakSelf.myData.addr.name = model.name;
            weakSelf.myData.addr.tel = model.tel;
            weakSelf.myData.addr.city = model.city;
            weakSelf.myData.addr.addr = model.addr;
            weakSelf.myData.addr.ID = model.addrId;
            [weakSelf.myTalbleView reloadData];
        };
        [self.navigationController pushViewController:addressListVC animated:YES];
    }else if (indexPath.section == 1 && indexPath.row == 2 && self.type == PayTypeProduct) {
        [self chooseCoupon];
    }else {
        [self.view endEditing:YES];
    }
}

#pragma mark -- mark EVENT

/**
 去充值

 @param sender sender
 */
- (void)rechargeButtonClick:(UIButton *)sender {
    XMRechargeViewController *vc = [[XMRechargeViewController alloc] init];
    SKWeakSelf;
    vc.paySuccessResultBlock = ^{
        // 刷新数据
        [weakSelf getBalanceInfo];
    };
    [self.navigationController pushViewController:vc animated:true];
}

/**
 选择优惠券
 */
- (void)chooseCoupon {
    // 选择 优惠券
    if (!(self.couponModels.count > 0)) {
        [self showMessage:@"暂无可用的优惠券"];
        return;
    }
    CouponViewController *vc = [[CouponViewController alloc] init];
    vc.couponModels = self.couponModels;
    SKWeakSelf;
    vc.chooseCouponResultBlock = ^(MyCouponDataModel * _Nonnull model) {
        weakSelf.couponModel = model;
        NSIndexPath *coupon = [NSIndexPath indexPathForRow:2 inSection:1]; // 优惠券
        NSIndexPath *totalMoeny = [NSIndexPath indexPathForRow:3 inSection:1]; // 总金额
        [weakSelf.myTalbleView reloadRowsAtIndexPaths:@[coupon, totalMoeny] withRowAnimation:UITableViewRowAnimationNone];
    };
    [self.navigationController pushViewController:vc animated:true];
}

/**
 选择支付方式

 @param sender sender
 */
- (void)choosePaytypeButtonClick:(UIButton *)sender {
    _balancePaybtn.selected = false;
    _aliPaybtn.selected = false;
    _wxPaybtn.selected = false;
    sender.selected = true;
    if (sender == _balancePaybtn) {
        // 余额支付
        
    }else if (sender == _aliPaybtn) {
        // 支付宝
        
    }else if (sender == _wxPaybtn) {
        // 微信支付
        
    }else {}
}

@end
